package org.example.controller;

import org.apache.ibatis.annotations.Param;
import org.example.domain.Activity;
import org.example.service.impl.ActivityServiceImpl;
import org.example.untils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("activity")
public class ActivityController {

    @Autowired
    private ActivityServiceImpl activityServiceImpl;

    @PostMapping("/addActivity")
    public Result addActivity(@RequestBody Activity activity){
        activityServiceImpl.addActivity(activity);
        return new Result(200,"上架成功",null);
    }

    @PostMapping("/updateActivityById")
    public Result updateActivityById(@RequestBody Activity activity){
        switch (activity.getTag()){
            case "思政培根":
                activity.setTag("0");
                break;
            case "学科启智":
                activity.setTag("1");
                break;
            case "体育强魄":
                activity.setTag("2");
                break;
            case "美育润心":
                activity.setTag("3");
                break;
            case "劳动强基":
                activity.setTag("4");
                break;
            case "创新创业":
                activity.setTag("5");
                break;
            case "其他":
                activity.setTag("6");
                break;
            default:
                activity.setTag("7");
        }
        activityServiceImpl.updateActivityInfoById(activity);
        return new Result(200,"更新成功",null);
    }

    @GetMapping("/getActivitiesByIsOver")
    public Result getActivitiesByIsOver(@Param("start") int start){
        Activity[] activities= activityServiceImpl.getActivitiesByIsOver(start);
        return new Result(200,"获取成功",activities);
    }

    @GetMapping("/getActivityById")
    public Result getActivityById(@Param("id") Integer id){
        Activity activity= activityServiceImpl.getActivityById(id);
        return new Result(200,"获取成功",activity);
    }

    @GetMapping("/getActivitiesByName")
    public Result getActivitiesByName(@Param("name") String name){
        Activity[] activities= activityServiceImpl.getActivitiesByName(name);
        return new Result(200,"获取成功",activities);
    }

    @GetMapping("/getActivitiesByNameButNoPicture")
    public Result getActivitiesByNameButNoPicture(@Param("name") String name,
                                                  @RequestParam(name = "tag",required = false) Integer tag){
        Activity[] activities= activityServiceImpl.getActivitiesByNameButNoPicture(name,tag);
        return new Result(200,"获取成功",activities);
    }

    @GetMapping("/getActivitiesByNameAndIsOver")
    public Result getActivitiesByNameAndIsOver(@Param("name") String name){
        Activity[] activities= activityServiceImpl.getActivitiesByNameAndIsOver(name);
        return new Result(200,"获取成功",activities);
    }

    @GetMapping("/downActivityById")
    public Result downActivityById(@Param("id") Integer id){
        activityServiceImpl.downActivityById(id);
        return new Result(200,"下架成功",null);
    }

}
